package com.codef.sharp.common.database.utils;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codef.sharp.common.core.enums.ResponseStatus;
import com.codef.sharp.common.core.exception.SharpException;
import com.codef.sharp.common.core.utils.RequestUtil;

import javax.servlet.ServletRequest;
import java.util.Objects;

/**
 * @author zhanrf
 * @date 2022/2/13 18:19
 */
public class PageUtil {

	private PageUtil() {}

	/**
	 * 获取分页数据
	 * @return IPage<T>
	 */
	public static <T> Page<T> getPage() {
		ServletRequest request = RequestUtil.getRequest();
		if (Objects.isNull(request)) {
			throw new SharpException("请求体不存在！");
		}
		// 当前页
		String currentStr = request.getParameter("current");
		// 每页数量
		String sizeStr = request.getParameter("size");
		if (StrUtil.isBlank(currentStr) || StrUtil.isBlank(sizeStr)) {
			throw new SharpException(ResponseStatus.METHOD_ARGUMENT_NOT_VALID);
		}
		long current = Long.parseLong(currentStr);
		long size = Long.parseLong(sizeStr);
		return new Page<>(current, size);
	}
}
